<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class My_time {

	var $CI;
    var $_model;
			
	public function __construct()
    {
      
        $CI =& get_instance();        
        $this->_model = $CI;
		$this->_model->load->database();
		$this->_model->load->model("muser");
		$this->_model->load->helper('date');		
    }

    public function makeMonthTime($Staff_ID,$start,$end)    
    {
    	
    	$result=$this->_model->muser->getMonthTime($Staff_ID,$start,$end);				
		$data=array(
			'Staff_ID'=>$Staff_ID,
			'Month'=> date("m", strtotime($start)),
			'Year'=> date("Y", strtotime($start)),
			'FromDate'=>$start,
			'ToDate'=>$end,
			'ULDay'=>'0',
			'SLDay'=>'0',
			'PLDay'=>'0',
			'ALDay'=>'0',
			'HourOff'=>'0',
			'OT_Weekday'=>'0',
			'ON_Weekday'=>'0',
			'Time_Saturday'=>'0',
			'OT_Saturday'=>'0',
			'ON_Saturday'=>'0',
			'ON_Sunday'=>'0',
			'OT_Sunday'=>'0',
			'OT_Holiday'=>'0',
			'ON_Holiday'=>'0'	
				
		);
		 
		$data['WorkedDay']=0;
		$data['WorkingDay']=round($this->_model->muser->getWorkingDay($start,$end));		
		
		
	if ($result!=NULL)
		{			
			foreach ($result as $row)
			{			
				if ( trim($row->DayType)=='HLD'){
					$data['OT_Holiday']=$data['OT_Holiday']+$row->WorkTime+$row->OT;
					$data['ON_Holiday']=$data['ON_Holiday'] + $row->ON;					
					$data['WorkedDay']=$data['WorkedDay']+1;
					continue ;				
				}
				if ( trim($row->DayType)=='UL'){
					$data['ULDay'] +=1;			
					continue ;
				}
				
				if ( trim($row->DayType)=='SL'){
					$data['SLDay'] +=1;					
					continue 	;
				}
				
				
				if ( trim($row->DayType)=='PL'){
					$data['PLDay'] +=1;					
					continue ;
				}
				if (trim($row->DayType)=='AL'){
					$data['ALDay'] +=1;					
					continue ;
				}				
				
				if (trim($row->WeekDay)=='Saturday'){
					if (trim($row->DayType)=='Night')						
						{
							$data['WorkedDay']+=1;	
							$data['Time_Saturday'] +=$row->WorkTime+$row->OT+$row->ON;
							continue ;
						}	
					else{
						$data['Time_Saturday'] +=$row->WorkTime;
						$data['OT_Saturday'] +=$row->OT;
						$data['ON_Saturday'] +=$row->ON;	
						continue;
					}				
							
				}
				
				if (trim($row->WeekDay)=='Sunday'){			
					if (trim($row->DayType)=='Night')
						{
							 $data['WorkedDay']+=1;							
							 $data['OT_Sunday'] += $row->WorkTime+$row->OT+$row->ON;			
							continue;			
						}else{
							$data['OT_Sunday'] +=$row->WorkTime+$row->OT;
							$data['ON_Sunday'] +=$row->ON;
						}		
				}
				
				
				if ($row->WorkTime>0) {
					 $data['WorkedDay']+=1;					
					 if ($row->WorkTime < 8){
						$data['HourOff'] = $data['HourOff']+ 8 -$row->WorkTime;
					}
				}
				
				$data['OT_Weekday'] += $row->OT;
				$data['ON_Weekday'] +=$row->ON;		
			}			
			
			
		}	
	 if ($this->_model->muser->checkMonthTime($data['Staff_ID'],$start,$end)){
				$this->_model->muser->addData('MonthTime',$data);
	 			$result=$this->_model->muser->getRMonthTime($data['Staff_ID'],$start,$end);
				return $result->MonthTime_ID;
	 							
	}else{
					$result=$this->_model->muser->getRMonthTime($data['Staff_ID'],$start,$end);
					$MonthTime_ID=$result->MonthTime_ID;
					$this->_model->muser->updateData('MonthTime','MonthTime_ID',$MonthTime_ID,$data);
					return $MonthTime_ID;											
			}
    
}


  public function makeMonthTimeAllStaff($FromDate,$ToDate){  
	$result=$this->_model->muser->getStaffID();	
	foreach ($result as $row){
		$Staff_ID=$row->Staff_ID;
		if ($this->_model->muser->checkContract($Staff_ID,$FromDate)){				
			continue;
		}
		
		$this->makeMonthTime($Staff_ID,$FromDate,$ToDate);		
	}
	
}
  
}
/* End of file Someclass.php */